<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.jbit.examSystem.pojo.User" %>
<%@ page import="com.jbit.examSystem.pojo.Student" %>
<%@ page import="java.util.Calendar" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<%@include file="../common/include-header.jsp" %>
<%@include file="../common/include-footer.jsp" %>
<body>
<form id="frmUpdateInfo" class="layui-form">
    <div class="layui-divStyle">
        <div class="user_left">
            <input class="layui-hide" name="id" value="${user.id}"/>
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" value="${user.name}" name="name"
                           class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-block">
                    <c:if test="${user.identityId == 10001}">
                        <input type="text" value="老师" name="name" disabled
                               class="layui-input layui-disabled">
                    </c:if>
                    <c:if test="${user.identityId == 10000}">
                        <input type="text" value="学生" name="name" disabled
                               class="layui-input layui-disabled">
                    </c:if>
                </div>
            </div>
            <c:if test="${user.identityId == 10000}">
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <select id="sex"  name="sex" style="display: initial;width: 100px;height: 30px">
                            <option value="1" ${user.sex ? 'selected':''} >男</option>
                            <option value="0" ${user.sex ? '':'selected'}>女</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">生日</label>
                    <div class="layui-input-block">
                        <input type="date" id="date_info" name="birthday" placeholder="请选择日期" lay-verify="birthday"
                               class="layui-input userBirthday">
                    </div>
                </div>
            </c:if>
            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-block">
                    <input id="phone" type="tel" value="${user.phone}" name="phone" placeholder="请输入手机号码" lay-verify="phone"
                           class="layui-input userPhone">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input id="email" type="text" value="${user.email}" name="email" placeholder="请输入邮箱" lay-verify="email" disabled
                           class="layui-input userEmail layui-disabled">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-left: 5%;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-filter="changeUser" onclick="updateInfo(${user.identityId})">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary restuserinfo">重置</button>
            </div>
        </div>
    </div>
</form>

<%
    String year = null;
    String month = null;
    String day = null;
    User user = (User) session.getAttribute("user");
    if (user.getIdentityId() == 10000){
        Student stu = (Student) user;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(stu.getBirthday());
        year = String.valueOf(calendar.get(Calendar.YEAR));
        month = String.valueOf(calendar.get(Calendar.MONTH));
        day = String.valueOf(calendar.get(Calendar.DATE));
    }
%>

<script>
    $(document).ready(function () {
        var day = ("0" + <%=day%>).slice(-2);
        var month = ("0" + (<%=month%> + 1)).slice(-2);
        var today = <%=year%> + "-" + (month) + "-" + (day);
        $('#date_info').val(today);
    })

    var prefix = "/system/user";

    function submitHandler() {
        var data = $("#form-user-update").serializeArray();
        var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
        $.operate.savePut(prefix + "/update", data);
    }

    function updateInfo(identityId){
        if (identityId == 10000){
            $.post(
                'student/updateInfo',
                $('#frmUpdateInfo').serialize(),
                function (result) {
                    if (result.success){
                        layer.msg('学员信息修改成功',{time:2000});
                    }
                }
            );
        }
        else if (identityId == 10001){
            $.post(
                'teacher/updateInfo',
                $('#frmUpdateInfo').serialize(),
                function (result) {
                    if (result.success){
                        layer.msg('教员信息修改成功',{time:2000});
                    }
                }
            );
        }

    }

</script>
</body>
</html>
